<?php

class Am_Grid_Action_Delete extends Am_Grid_Action_Abstract
{
    protected $privilege = 'delete';
    protected $title;
	protected $isJob = false;
	protected $apiNeo;
    
	public function setJob($isJob)
	{
		$this->isJob = $isJob;
	}
	public function setAPI($api_key, $api_type, $api_switch)
	{
		$this->apiNeo = Am_Di::getInstance()->apineo;
		$this->apiNeo->setAPIFull($api_key, $api_type, $api_switch);
	}
    public function __construct($id = null, $title = null)
    {
        $this->title = ___("Delete %s");
        $this->attributes['data-confirm'] = ___("Do you really want to delete record?");
		
        parent::__construct($id, $title);
    }
    public function run()
    {
        if ($this->grid->getRequest()->get('confirm'))
		{
			
            $this->delete();
		}
        else
		{
			if($this->isJob)
			{
				$apiNeo = Am_Di::getInstance()->apineo;
				$data = $apiNeo->checkAPIKey();	
				if($data)
				echo $this->renderConfirmation ();	
				else
				{
					echo $this->renderError();
					//sleep(5);
					//$this->grid->redirectBack();	
				}	
			}else{
				echo $this->renderConfirmation ();	
			}
			
			
		}
            
    }
    public function delete()
    {
		
        $record = $this->grid->getRecord();
        $args = array( $record, $this->grid );
        $this->grid->runCallback(Am_Grid_Editable::CB_BEFORE_DELETE, $args);
		if($this->isJob)
		{	
			$result = $this->apiNeo->deleteAPIData($record->job_id_server);		
			
		}
        $this->grid->getDataSource()->deleteRecord($this->grid->getRecordId(), $record);
	
        $this->grid->runCallback(Am_Grid_Editable::CB_AFTER_DELETE, $args);
        $this->log();
        $this->grid->redirectBack();
    }
        
}